package com.youyiwen.dto;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhaoyang
 * @Date: 2021/03/21
 */
@Data
public class PageInitDTO {
    private List<QuestionDTO> questions;
    private boolean showPrevious;
    private boolean showFirstPage;
    private boolean showNext;
    private boolean showEndPage;

    private Integer totalPage;
    private Integer page;
    private List<Integer> pages = new ArrayList<Integer>() {
    };

    public void setPageInition(Integer totolCount, Integer page, Integer size) {
        this.page = page;

        Integer totolPage;
        if (totolCount % size == 0) {
            totolPage = totolCount / size;
        } else {
            totolPage = totolCount / size + 1;
        }
        this.totalPage = totolPage;
        pages.add(page);
        for (int i = 1; i <= 3; i++) {
            if (page - i > 0) {
                pages.add(0, page - i);
            }
            if (page + i <= totolPage) {
                pages.add(page + i);
            }
        }

        //展示上一页
        if (page == 1) {
            showPrevious = false;
        } else {
            showPrevious = true;
        }
        //展示下一页
        if (page == totolPage) {
            showNext = false;
        } else {
            showNext = true;
        }

        //是否展示第一页
        if (pages.contains(1)) {
            showFirstPage = false;
        } else {
            showFirstPage = true;
        }

        //最后一页
        if (pages.contains(totolPage)) {
            showEndPage = false;
        } else {
            showEndPage = true;
        }

    }
}
